﻿/*  
cd /projects/hsieh_project/proj_201809/code_1_data/
qsas data_1_mkt_ind_mktsize.sas 5 &
*/

/*
Author: Adarsh Kumar
Objective: Calculate Average Market Size for all and top firms defined by market (est)
*/

libname hr "/projects/hsieh_project/proj_201809/data/";

%Let dir_out = /projects/hsieh_project/proj_201809/data/;

/*
================================================================================
Load Data
*/

%include "/projects/hsieh_project/proj_201809/code_1_data/m_read.sas" /source2;

%m_read(param_dev=1,param_lyear=%quote(1977,2013),param_czone=0,param_msa1983=1,param_msacz=0,param_drop=1);

%include "/projects/hsieh_project/code_0_general/m_perc_by_var.sas" /source2;


/* Macro that calculates average market size of markets/cities served by each firm */
%macro mktsize(dt_in_c = , dt_out_c = , c_city = ); 

%put City Variable &c_city.;

/* First find the mean employment in that city every year */

data dt_city; 
  set &dt_in_c.;
run;

proc sort data=dt_city; by year &c_city.; run;

proc means data=dt_city noprint;
  by year &c_city.;
  output out=dt_city(drop=_type_ _freq_) sum(worker)=city_emp;
run;

/* Generate ch_ind-firm-c_city level data to merge with. */
data dt_indf; 
  set &dt_in_c.;
run;

proc sort data=dt_indf; by year ch_ind firmnum &c_city.; run;

data dt_indf;
  set dt_indf; 
  by year ch_ind firmnum &c_city.;
  if first.&c_city.;
  n_&c_city. = 1;
run;

/* Merge dt_indf dt_city to get market size for each firm's city/market served */
proc sort data=dt_city; by year &c_city.; run;
proc sort data=dt_indf; by year &c_city.; run;

data dt_indf_city;
  merge dt_indf dt_city; 
  by year &c_city;
run;

/* Take mean by ind-firm to get average market size that each ind-firm is present in */

proc sort data=dt_indf_city;
  by year ch_ind firmnum;
run;

proc means data=dt_indf_city noprint;
  by year ch_ind firmnum;
  output out=&dt_out_c.(drop=_type_ _freq_) mean(city_emp)=mkt_&c_city. sum(city_emp n_&c_city.)=mkts_&c_city. n_&c_city.;
run;

%mend;


/*---------------------------------------------------------*/
/* Define macro that aggregates to desired level */
/*-------------------------------------------------------*--*/

%macro mkt_ind_stat(dt_in = lbd, dt_out = ,l_perc = 10, ivar = lbdid, ivaro = n_est);

%put Input &dt_in.;
%put Output &dt_out.;
%put Percentile &l_perc.;
%put Top Firm Definition &ivar.;
%put # of Mkts variable label &ivaro.;

%put Mkt variable used to define top firm;
%put Keeping first obs of Year Ind Firm Mkt variable;

data dt_var;
  set lbd;
run;

proc sort data= dt_var; by year ch_ind firmnum &ivar.;
data dt_var_for_perc; 
  set dt_var;
  by year ch_ind firmnum &ivar.;
  if first.&ivar.;
  &ivaro. = 1;
run;
proc sort data = dt_var_for_perc; by year ch_ind firmnum &ivar.;
%m_perc_by_var(ds_in = dt_var_for_perc, ds_out = dt_perc, var_in = &ivaro., var_by=%bquote(year ch_ind), var_unit=firmnum, var_out=mkt, var_runif=runif);


/* Percentiles have been computed at the industry - firm level. */

/* Call to mktsize macro to get average mktsize of each firm for msa1983, fips, zipcode: */
%mktsize(dt_in_c = dt_var, dt_out_c = dt_mktsize_msa1983, c_city = msa1983);
%mktsize(dt_in_c = dt_var, dt_out_c = dt_mktsize_fips, c_city = fips);

data dt_var_zip;
  set dt_var; 
  if zipcode = "" then delete;
  zipcode = substr(zipcode,1,5);
run;

%mktsize(dt_in_c = dt_var_zip, dt_out_c = dt_mktsize_zip, c_city = zipcode);

/* Merge mktsize datasets with dt_perc = ch_ind-firm level percentiles by # est */
proc sort data=dt_perc; by year ch_ind firmnum; 
proc sort data=dt_mktsize_msa1983; by year ch_ind firmnum; 
proc sort data=dt_mktsize_fips; by year ch_ind firmnum; 
proc sort data=dt_mktsize_zip; by year ch_ind firmnum; 

data dt_ind_firm; 
  merge dt_perc dt_mktsize_msa1983 dt_mktsize_fips dt_mktsize_zip;
  by year ch_ind firmnum;
run;

data dt_ind_firm;
  set dt_ind_firm;
  n_ind = 1;
run;

/*
--------------------------------------------------------------------------------
Get mean market size of all, top firms
*/


%Let l_ind_var_sum = n_ind mkts_msa1983 mkts_fips mkts_zip;
%Let l_ind_var_mean = mkt_msa1983 mkt_fips mkt_zip;

%Let i_list = 1;
%do %while (%scan(%bquote(&l_perc), &i_list) ~= );
  %Let i_perc=%scan(%bquote(&l_perc), &i_list.);
  %put Top &i_perc.% Industry Firms;
  
  data dt_ind_firm;
    set dt_ind_firm;
    mkt_msa1983_&i_perc.=.;
    mkt_fips_&i_perc.=.;
    mkt_zip_&i_perc.=.;
    mkts_msa1983_&i_perc.=.;
    mkts_fips_&i_perc.=.;
    mkts_zip_&i_perc.=.;
    n_ind_&i_perc. = .;
    
    if mkt_perc <= %sysevalf(&i_perc. / 100) then
      do;
	mkt_msa1983_&i_perc.= mkt_msa1983;
	mkt_fips_&i_perc.= mkt_fips;
	mkt_zip_&i_perc.= mkt_zip;
	mkts_msa1983_&i_perc.= mkts_msa1983;
	mkts_fips_&i_perc.= mkts_fips;
	mkts_zip_&i_perc.= mkts_zip;
	n_ind_&i_perc. = n_ind;
      end;
  run;
  
  %Let l_ind_var_sum = &l_ind_var_sum. n_ind_&i_perc. mkts_msa1983_&i_perc. mkts_fips_&i_perc. mkts_zip_&i_perc.;
  %Let l_ind_var_mean = &l_ind_var_mean. mkt_msa1983_&i_perc. mkt_fips_&i_perc. mkt_zip_&i_perc.;
  
  %Let i_list = %eval(&i_list + 1);
  
%end;

%put List of Variables for Top Firms;
%put &l_ind_var_mean.;
%put &l_ind_var_sum.;


/*
--------------------------------------------------------------------------------
Aggregate to year-industry level
*/

proc sort data=dt_ind_firm; by year ch_ind;

proc means data=dt_ind_firm noprint;
  by year ch_ind;
  output out=&dt_out.(drop = _type_ _freq_) sum(&l_ind_var_sum.) = &l_ind_var_sum. mean(&l_ind_var_mean.) = &l_ind_var_mean.;
run;

%mend;

/*Call to macro: */
%mkt_ind_stat(dt_in = lbd, dt_out = dt_est_mktsize ,l_perc = 10, ivar = lbdid, ivaro = n_est);

proc export data = dt_est_mktsize outfile = "&dir_out./mkt_ind_sum_mktsize.dta" replace;
run;

/* End of SAS file */

